/**
 * SpringMVC restful 风格
 * 财务管理 中 地址管理
 * @author yanwl
 * @date 2015-03-12
 */
package com.chnbst.bstyun.controller.user;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.chnbst.bstyun.core.common.SignUtil;
import com.chnbst.bstyun.model.pojo.Address;
import com.chnbst.bstyun.model.pojo.Province;
import com.chnbst.bstyun.model.pojo.User;
import com.chnbst.bstyun.model.vo.AddressVo;
import com.chnbst.bstyun.service.AddressService;
import com.chnbst.bstyun.service.PlaceService;
import com.chnbst.bstyun.service.UserService;

/**
 * 控制器注解 请求映射注解 controller 代表类是控制器 restcontroller 代表类是控制器， 所有的方法 都是ajax方法
 */
@Controller
@RequestMapping("/finance/address")
public class AddressController {
	@Autowired
	private UserService userService;
	@Autowired
	private AddressService addressService;
	@Autowired
	private PlaceService placeService;
	
	/**
	 * @return 地址管理  主页面  /finance/address/addressList
	 * @throws Exception  
	 */ 
	@RequestMapping(value = "/addressList", method = RequestMethod.GET)
	public ModelAndView addressList(HttpServletRequest request) throws Exception {
		User user = (User)request.getSession().getAttribute("userInfo");
		
		ModelAndView mav = new ModelAndView();
		List<AddressVo> addressVos = addressService.selectAddressVos(user.getUserId());
		mav.addObject("addressVos", addressVos);
		mav.setViewName("users/finance_manage/invoice_manage/address_list");
		return mav;
	}
	
	/**
	 * 增加或者修改地址
	 * @return boolean
	 */ 
	@RequestMapping(value = "/updateAddress", method = RequestMethod.POST)
	@ResponseBody
	public boolean updateAddress(String adrId,String faddname,Integer faddpro,
			Integer faddcity,Integer fadddis,String faddadd,Integer faddpost,
			String faddtel,Integer setDefault,HttpServletRequest request){
		Address address = null;
		if(SignUtil.isNotEmpty(adrId)) {
			address = addressService.selectAddress(Integer.valueOf(adrId));
		}else {
			address = new Address();
			User user = (User)request.getSession().getAttribute("userInfo");
			address.setUserId(user.getUserId());
		}
		
		address.setAreaCode(fadddis);
		address.setCityCode(faddcity);
		address.setProvinceCode(faddpro);
		address.setIsDefault(setDefault);
		address.setPostCode(faddpost);
		address.setReceiverName(faddname);
		address.setStreetAddress(faddadd);
		address.setUserTel(faddtel);
		address.setAddTime(new Date());
		
		boolean isSuccess = false;
		//设置为默认地址
		if(setDefault == 1) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("userId", address.getUserId());
			map.put("isDefault", 1);
			AddressVo aVo = addressService.selectAddressVoByMap(map);
			if(aVo != null) {
				//用户之前有默认地址
				aVo.setIsDefault(0);
				addressService.updateAddress(aVo);
			}
		}
		
		if(SignUtil.isNotEmpty(adrId)) {
			/*修改地址*/
			isSuccess = addressService.updateAddress(address);
		}else {
			/*新增地址*/
			isSuccess = addressService.insertAddress(address);
		}
		return isSuccess;
	}
	
	/**
	 * 地址详情
	 * @return Map<String, Object>
	 */ 
	@RequestMapping(value = "/addressDetail", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addressDetail(Integer addressId,HttpServletRequest request){
		Address address = addressService.selectAddress(addressId);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("address", address);
		return map;
	}
	
	/**
	 * 删除地址
	 * @return boolean
	 */ 
	@RequestMapping(value = "/deleteAddress", method = RequestMethod.POST)
	@ResponseBody
	public boolean deleteAddress(Integer addressId,HttpServletRequest request){
		Address address = addressService.selectAddress(addressId);
		address.setIsDelete(1);
		boolean isSuccess = addressService.updateAddress(address);
		return isSuccess;
	}
	
	/**
	 * 设置默认地址
	 * @return boolean
	 */ 
	@RequestMapping(value = "/setDefalutAddress", method = RequestMethod.POST)
	@ResponseBody
	public boolean setDefalutAddress(Integer addressId,HttpServletRequest request){
		User user = (User)request.getSession().getAttribute("userInfo");
		boolean isSuccess = addressService.setDefalutAddress(user.getUserId(), addressId);
		return isSuccess;
	}
	
	/**
	 * 检测地址总数
	 * @return boolean
	 */ 
	@RequestMapping(value = "/checkAddressRows", method = RequestMethod.POST)
	@ResponseBody
	public boolean checkAddressRows(HttpServletRequest request){
		User user = (User)request.getSession().getAttribute("userInfo");
		int rows = addressService.selectAddressRows(user.getUserId());
		boolean isAdd = true;
		if(rows >= 5) {
			isAdd = false;
		}
		return isAdd;
	}
	
	/**
	 * 获取省份列表
	 * @return boolean
	 */ 
	@RequestMapping(value = "/getProvinces", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getProvinces(HttpServletRequest request){
		List<Province> provinces = placeService.getProvinceList();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("provinces", provinces);
		return map;
	}
}
